﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;

namespace Mota.XMLControls
{
    public class MyDxRangeTrackBar : RangeTrackBarControl
    {
        public event PropertyChangedEventHandler XmlItemsChanged;
        public MyDxRangeTrackBar()
            : base()
        {
            this.ValueChanged += MyDxCheckedComboBox_EditValueChanged;
        }

        void MyDxCheckedComboBox_EditValueChanged(object sender, EventArgs e)
        {
            this.OnItemsChanged(null);
        }

        //Makes this property bindable.
        [Bindable(true)]
        public string XmlItems
        {
            get
            {

                XElement items = new XElement("Items",new XElement("Min",this.Value.Minimum.ToString())
                                                     ,new XElement("Max",this.Value.Maximum.ToString()));
                return items.ToString();
            }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    XElement items = XElement.Parse(value);
                    int min,max;
                    if (int.TryParse(items.Element("Min").Value, out min) &&
                        int.TryParse(items.Element("Max").Value, out max))
                        this.Value = new TrackBarRange(min, max);
                    
                }
            }
        }
        public void OnItemsChanged(PropertyChangedEventArgs e)
        {
            //Raise the event
            if (XmlItemsChanged != null)
                XmlItemsChanged(this, e);
        }
    }
}
